package me.spring.cloud.common.components.ips.common;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.spring.cloud.common.components.util.constant.CharConstant;

/**
 * data block class
 */
@Data
@NoArgsConstructor
public class DataBlock {

  /**
   * city id
   */
  private int city_id;

  /**
   * region address
   */
  private String region;


  private String country;
  private String province;
  private String city;
  private String area;
  private String isp;


  /**
   * region ptr in the db file
   */
  private int dataPtr;

  /**
   * construct method
   *
   * @param city_id
   * @param region  region string
   * @param dataPtr     data ptr
   */
  public DataBlock(int city_id, String region, int dataPtr) {
    this.city_id = city_id;
    this.region = region;
    this.dataPtr = dataPtr;
    String[] regionArrsy = StrUtil.splitToArray(region, CharConstant.PIPE);
    this.country = regionArrsy[0];
    this.province = regionArrsy[1];
    this.city = regionArrsy[2];
    this.area = regionArrsy[3];
    this.isp = regionArrsy[4];
  }

  public DataBlock(int city_id, String region) {
    this(city_id, region, 0);
  }

  public DataBlock setCityId(int city_id) {
    this.city_id = city_id;
    return this;
  }

  public DataBlock setRegion(String region) {
    this.region = region;
    return this;
  }

  public DataBlock setDataPtr(int dataPtr) {
    this.dataPtr = dataPtr;
    return this;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(city_id).append('|').append(region).append('|').append(dataPtr);
    return sb.toString();
  }
}
